package com.liu.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.liu.demo.dao.DictRepository;
import com.liu.demo.dao.MemberRepository;
import com.liu.demo.dao.OrderMapper;
import com.liu.demo.dao.UserMapper;
import com.liu.demo.domain.po.Dict;
import com.liu.demo.domain.po.Member;
import com.liu.demo.domain.po.Order;
import com.liu.demo.domain.po.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping
public class DemoController {

    @Resource
    private OrderMapper orderMapper;
    @Resource
    private UserMapper userMapper;

    @Resource
    private DictRepository dictRepository;

    @Resource
    private MemberRepository memberRepository;

    @GetMapping("save")
    public void save() {
        Order order = new Order();
        String code = String.valueOf(System.currentTimeMillis());
        order.setCode(code);

        orderMapper.insert(order);
        log.info("save success:{}", order.getId());
    }

    @GetMapping("user/save")
    public void saveUser() {
        User u = new User();
        u.setName(String.valueOf(System.currentTimeMillis()));

        userMapper.insert(u);
        log.info("save user success:{}", u.getId());
    }

    @GetMapping("dict/save")
    public void saveDict() {
        Dict d = new Dict();
        d.setCode(String.valueOf(System.currentTimeMillis()));

        dictRepository.insert(d);
        log.info("save dict success:{}", d.getId());
    }

    @GetMapping("member/save")
    public void saveMember() {
        Member m = new Member();
        m.setName("liujiazhong");

        memberRepository.insert(m);
        log.info("save member success:{}", m.getId());
    }

    @GetMapping("get/{id}")
    public Order getOne(@PathVariable Long id) {
        return orderMapper.selectById(id);
    }

    @GetMapping("getDict/{id}")
    public Dict getDict(@PathVariable Long id) {
        return dictRepository.selectById(id);
    }

    @GetMapping("get_member/{id}")
    public Member getMember(@PathVariable Long id) {
        return memberRepository.selectById(id);
    }

    @GetMapping("get_by_code/{code}")
    public Order getByCode(@PathVariable String code) {
        return orderMapper.selectOne(new LambdaQueryWrapper<Order>().eq(Order::getCode, code));
    }

}
